package apy.codecase.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import apy.codecase.model.event.MessageEvent;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by apy on 2017/7/28.
 */

public abstract class BaseFragment<K, V> extends Fragment implements BaseView<K, V> {

    public BasePresenterImpl presenter;
    public View rootView;
    public boolean hasLoadData;
    private Unbinder unbinder;

    @Override
    public void afterLoadDataAsObject(Object t) {
        hasLoadData = true;
    }

    @Override
    public void afterLoadData(K t, V o) {
        hasLoadData = true;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(initLayout(), container, false);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void initData() {

    }

    protected abstract int initLayout();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        /* Do something */
    }

    public void hasLoad(){
        hasLoadData = false;
    }

    public void loadNetData(){

    }

    @Override
    public void afterLoadData(K t) {

    }
}
